<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('contact.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('contact.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('contact.name')" prop="name">
                    <el-input v-model="dataForm.name" :placeholder="i18n('common.please_input') + i18n('contact.name')" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="$t('contact.mobile')" prop="mobile">
                    <el-input v-model="dataForm.mobile" :placeholder="i18n('common.please_input') + i18n('contact.mobile')" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="$t('contact.position')" prop="position">
                    <el-input v-model="dataForm.position" :placeholder="i18n('common.please_input') + i18n('contact.position')" clearable></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('contact.sex')" prop="sex">
                    <el-select
                      v-model="dataForm.sex"
                      :placeholder="i18n('common.please_select') + i18n('contact.sex')" clearable>
                      <el-option v-for="(v, k) in sexList" :label="v.text" :key="k" :value="v.value"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('contact.marriage_state')" prop="marriage_state">
                    <el-select
                      v-model="dataForm.marriage_state"
                      :placeholder="i18n('common.please_select') + i18n('contact.marriage_state')" clearable>
                      <el-option v-for="(v, k) in marriageStateList" :label="v.text" :key="k" :value="v.value"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('contact.is_principal')" prop="is_principal">
                    <el-switch
                      v-model="dataForm.is_principal"
                      active-value="1"
                      :active-text="i18n('common.yes')"
                      inactive-value="2"
                      :inactive-text="i18n('common.no')">
                    </el-switch>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('contact.working_hours')" prop="working_hours">
                    <el-input v-model="dataForm.working_hours" :placeholder="i18n('common.please_input') + i18n('contact.working_hours')" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('contact.brithday')" prop="brithday">
                    <el-date-picker v-model="dataForm.brithday" type="date"  :placeholder="i18n('common.please_input') + i18n('contact.brithday')" clearable/>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('contact.is_policymaker')" prop="is_policymaker">
                    <el-switch
                      v-model="dataForm.is_policymaker"
                      active-value="1"
                      :active-text="i18n('common.yes')"
                      inactive-value="2"
                      :inactive-text="i18n('common.no')">
                    </el-switch>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('contact.child_total')" prop="child_total">
                    <el-input v-model="dataForm.child_total" :placeholder="i18n('common.please_input') + i18n('contact.child_total')" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('contact.relation')" prop="relation">
                    <el-input v-model="dataForm.relation" :placeholder="i18n('common.please_input') + i18n('contact.relation')" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('contact.interest')" prop="interest">
                    <el-input v-model="dataForm.interest" :placeholder="i18n('common.please_input') + i18n('contact.interest')" clearable></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('contact.value_concept')" prop="value_concept">
                    <el-input v-model="dataForm.value_concept" :placeholder="i18n('common.please_input') + i18n('contact.value_concept')" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('common.sort')" prop="sort">
                    <el-input-number v-model="dataForm.sort" :min="0">
                    </el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('common.status')" prop="status">
                    <el-switch
                      v-model="dataForm.status"
                      active-value="1"
                      :active-text="i18n('common.enable')"
                      inactive-value="2"
                      :inactive-text="i18n('common.disable')">
                    </el-switch>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="24">
                  <el-form-item :label="i18n('contact.remark')" prop="remark">
                    <el-input
                      type="textarea"
                      v-model="dataForm.remark"
                      :rows="3"
                      show-word-limit
                      maxlength="200"
                      :placeholder="i18n('common.please_input') + i18n('contact.remark')">
                    </el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="contact-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/contact')

  const sexList = ref([])
  const marriageStateList = ref([])

  const dataForm = reactive({
    id: 0,
    client_id: 0,
    name: '',
    sex: '',
    mobile: '',
    position: '',
    brithday: '',
    working_hours: '',
    marriage_state: '',
    child_total: '',
    is_principal: '',
    is_policymaker: '',
    relation: '',
    interest: '',
    value_concept: '',
    remark: '',
    sort: 0,
    status: '1',
  })

  const dataRule = reactive({
    name: [{
      required: true,
      message: i18n('contact.rules.name.require'),
      trigger: 'blur',
    }],
    sex: [{
      required: true,
      message: i18n('contact.rules.sex.require'),
      trigger: 'blur',
    }],
    mobile: [{
      required: true,
      message: i18n('contact.rules.mobile.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.name = data.data.name
            dataForm.sex = data.data.sex.value + ''
            dataForm.mobile = data.data.mobile
            dataForm.position = data.data.position
            dataForm.brithday = data.data.brithday
            dataForm.working_hours = data.data.working_hours
            dataForm.marriage_state = data.data.marriage_state.value + ''
            dataForm.child_total = data.data.child_total
            dataForm.is_principal = data.data.is_principal.value + ''
            dataForm.is_policymaker = data.data.is_policymaker.value + ''
            dataForm.relation = data.data.relation
            dataForm.interest = data.data.interest
            dataForm.value_concept = data.data.value_concept
            dataForm.remark = data.data.remark
            dataForm.sort = data.data.sort
            dataForm.status = data.data.status.value + ''
          }
        })
      }
    })
  }


  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate((valid) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/handle`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            client_id: dataForm.client_id,
            name: dataForm.name,
            sex: dataForm.sex,
            mobile: dataForm.mobile,
            position: dataForm.position,
            brithday: dataForm.brithday,
            working_hours: dataForm.working_hours,
            marriage_state: dataForm.marriage_state,
            child_total: dataForm.child_total,
            is_principal: dataForm.is_principal,
            is_policymaker: dataForm.is_policymaker,
            relation: dataForm.relation,
            interest: dataForm.interest,
            value_concept: dataForm.value_concept,
            remark: dataForm.remark,
            sort: dataForm.sort,
            status: dataForm.status,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }


  const loadSexList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'user_sex_type',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        sexList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }


  const loadMarriageStateList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'marriage_state',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        marriageStateList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  if(query.client_id) {
    dataForm.client_id = parseInt(query.client_id)
  }

  onMounted(() => {
    init(formRef)

    loadSexList()
    loadMarriageStateList()
  })
</script>
